package veating.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import veating.bean.Admin;
import veating.dao.AdminDao;
import veating.service.Dbcp;


public class AdminDaoImpl implements AdminDao {

	@Override
	public int update(int aid, Admin admin) {
		int result = 0;
		String sql = "update admin set name=?,password=?,gender=?,phone=? where aid=?";
		QueryRunner qr = new QueryRunner(Dbcp.getDataSource());
		Object[] params = {admin.getName(),admin.getPassword(),admin.getGender(),admin.getPhone(),aid};
		try {
			result = qr.update(sql,params);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}

	public Admin findByPhone(String phone){
		Admin admin = new Admin();
		String sql="select * from admin where phone=?";
		QueryRunner qr = new QueryRunner(Dbcp.getDataSource());
		try {
			admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), phone);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
		return admin;
	}
	@Override
	public boolean login(String phone,String password){
		
		Admin admin = findByPhone(phone);
		if(admin!=null){
			if(password.equals(admin.getPassword())){
				return true;
			}
			else
				return false;
		}
		else
		return false;
	}

	@Override
	public boolean logout() {
		
		return true;
	}


	}

